<?php

/**
 * contact : 刘经理 19326416371
 * company : 版权所属 四川北星惯牌科技有限公司
 **/

namespace app\common\manager;

use app\common\model\Authentication;
use app\common\model\CaptainApplication;
use app\common\model\Goods;
use app\common\model\User;
use think\Db;
use think\Exception;

class GoodsManager extends BaseManager
{
    /**
     * 获取商品列表
     * @param int $page
     * @param int $step
     * @return array
     */
    public static function List(int $page=1,int $step=10):array
    {
        $return = ['total'=>0,'totalPage'=>1,'currentPage'=>$page,'list'=>[]];
        if ($page <= 0){
            $page = 1;
        }
        try {

            $goodsModel = new Goods();
            $start = ($page - 1) * $step;

            $count = $goodsModel->alias('g')
                ->join('product p','g.pdu_id = p.pdu_id','left')
                ->join('level l','g.level_id = l.vl_id','left')
                ->where('g.status',Goods::STATUS_ENABLE)->where('g.shelves',Goods::SHELVES_YES)
                ->count();

            $goods = $goodsModel->alias('g')
                ->join('product p','g.pdu_id = p.pdu_id')
                ->join('level l','g.level_id = l.vl_id')
                ->where('g.status',Goods::STATUS_ENABLE)->where('g.shelves',Goods::SHELVES_YES)
                ->order('g.g_id desc')
                ->field('g.*,p.name,p.cover_image,p.thumbnail,p.introduce,p.detail,l.vip_name')
                ->limit($start,$step)
                ->select();
            if ($goods){
                foreach ($goods as $k=>$v){
                    if ($v['cover_image'] && strrpos($v['cover_image'],'https') === false){
                        $goods[$k]['cover_image'] = cdnurl($v['cover_image']);
                    }else{
                        config('app.api_domain').$v['cover_image'];
                    }

                    if ($v['thumbnail'] && strrpos($v['thumbnail'],'https') === false){
                        $goods[$k]['thumbnail'] = cdnurl($v['thumbnail']);
                    }else{
                        config('app.api_domain').$v['thumbnail'];
                    }
                }
            }
            $pageData = page($count,$step,$page);

            $return['list'] = $goods;
            $return = array_merge($return,$pageData);
            self::setStatusCode(self::$successCode);
            self::setMessage(self::$successMessage);
            self::setData($return);
        }catch (Exception $exception){
            self::setStatusCode(self::$failCode);
            self::setMessage($exception->getMessage());
            self::setData($return);
        }

        return ['code'=>self::getStatusCode(),'message'=>self::getMessage(),'data'=>self::getData()];
    }

}